<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('主子表提交')"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <th:block th:include="include :: select2-css"/>
    <style>
        input::-webkit-outer-spin-button,
        input::-webkit-inner-spin-button {
            -webkit-appearance: none;
        }

        input[type="number"] {
            -moz-appearance: textfield;

        }

    </style>

</head>


<body class="gray-bg">

<div class="main-content">
    <form id="form-add" class="form-horizontal">
        <div class="row">
            <div class="col-sm-12">
                <button type="button" class="btn btn-white btn-sm" onclick="addColumn()"><i class="fa fa-plus"> 增加</i>
                </button>
                <button type="button" class="btn btn-white btn-sm" onclick="delColumn()"><i class="fa fa-minus">
                    删除</i></button>
                <div class="col-sm-12 select-table table-striped">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </form>
</div>

<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存
        </button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭
        </button>
    </div>
</div>

<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: select2-js"/>
<script th:src="@{/js/jquery.tmpl.js}"></script>
<script th:inline="javascript">
    var prefix = ctx + "biPlan";
    /* 初始化 数据 */
    var compName = [[${comp}]];
    var years = [[${years}]];
    var months = [[${months}]];
    $(function () {
        var options = {
            pagination: false,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            sidePagination: "client",
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'index',
                    align: 'center',
                    title: "序号",
                    formatter: function (value, row, index) {
                        var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                        return columnIndex + $.table.serialNumber(index);
                    }
                },
                {
                    field: 'compName',
                    align: 'center',
                    title: "工程公司",
                    formatter: function (value, row, index) {
                        var html = '';
                        html += '<select class="form-control" name="con[%s].compId" id="compName' + index + '" onchange="getTime(' + index + ')"><option value="">请选择</option>';
                        for (var i = 0; i < compName.length; i++) {
                            html += '<option title=' + compName[i].compId + ' value=' + compName[i].compId
                            if (compName[i].compId == value) {
                                html += ' selected ';
                            }
                            html += '>' + compName[i].compName + '</option>'
                        }
                        html += '</select>';
                        return $.common.sprintf(html, index, value);
                    }
                },
                {
                    field: 'years',
                    align: 'center',
                    title: "年份",
                    formatter: function (value, row, index) {
                        var html = '';
                        html += '<select class="form-control" name="con[%s].years" id="years' + index + '" onchange="getTime(' + index + ')"><option value="">请选择</option>';
                        for (var i = 0; i < years.length; i++) {
                            html += '<option value=' + years[i].years
                            if (years[i].years == value) {
                                html += ' selected ';
                            }
                            html += '>' + years[i].years + '年</option>'
                        }
                        html += '</select>';
                        return $.common.sprintf(html, index, value);
                    }
                },
                {
                    field: 'months',
                    align: 'center',
                    title: "月份",
                    formatter: function (value, row, index) {
                        var html = '';
                        html += '<select class="form-control" name="con[%s].months" id="months' + index + '" onchange="getTime(' + index + ')"><option value="">请选择</option>';
                        for (var i = 0; i < months.length; i++) {
                            html += '<option value=' + months[i].monthInYear
                            if (months[i].monthInYear == value) {
                                html += ' selected ';
                            }
                            html += '>' + months[i].monthInYear + '月</option>'
                        }
                        html += '</select>';
                        return $.common.sprintf(html, index, value);
                    }
                },
                {
                    field: 'planBackPay',
                    align: 'center',
                    title: "计划回款",
                    formatter: function (value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='number' placeholder='请输入计划回款' name='con[%s].planBackPay' value='%s' autocomplete=\"off\" required>", index, value);
                        return html;
                    }
                },
                {
                    field: 'realityBackPayIn',
                    align: 'center',
                    title: "实际内部回款",
                    formatter: function (value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='number' placeholder='请输入实际内部回款' name='con[%s].realityBackPayIn' value='%s' autocomplete=\"off\" required>", index, value);
                        return html;
                    }
                },
                {
                    field: 'realityBackPayOut',
                    align: 'center',
                    title: "实际外部回款",
                    formatter: function (value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='number' placeholder='请输入实际外部回款' name='con[%s].realityBackPayOut' value='%s' autocomplete=\"off\" required>", index, value);
                        return html;
                    }
                },
                {
                    field: 'realityBackPayTotal',
                    align: 'center',
                    title: "实际总回款",
                    formatter: function (value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='number' placeholder='请输入实际总回款' name='con[%s].realityBackPayTotal' value='%s' autocomplete=\"off\" required>", index, value);
                        return html;
                    }
                }
            ]
        };
        $.table.init(options);
    });

    function addColumn() {
        var row = {
            compName: "",
            years: "",
            months: "",
            planBackPay: 0,
            realityBackPayIn: 0,
            realityBackPayOut: 0,
            realityBackPayTotal: 0,
        }
        sub.addColumn(row);
        $("select").select2();
    }


    function getTime(index) {
        sub.editColumn();
        var data = $('#bootstrap-table').bootstrapTable('getData');
        if ($("#years" + index).val() != "" && $("#months" + index).val() != "" && $("#compName" + index).val() != "") {
            for (var i = 0; i < data.length; i++) {
                if (i != index) {
                    if ($("#years" + index).val() == data[i].years && $("#months" + index).val() == data[i].months && $("#compName" + index).val() == data[i].compName) {
                        $("#months" + index).val("").select2();
                        $.modal.alertWarning("存在重复时间，请重新选择！");
                    }
                }
            }
        }
        $("select").select2();
    }

    function delColumn() {
        sub.delColumn();
        $("select").select2();
    }

    function judge(index) {
        if ($("#compName" + index).val() == "" || $("#years" + index).val() == "" || $("#months" + index).val() == "") {
            return false;
        }
        return true;
    }

    function validform(formId) {
        return $(formId).validate()
    }

    function submitHandler() {
        if (validform("#form-add").form()) {
            sub.delColumn();
            var dataList = $('#bootstrap-table').bootstrapTable('getData');
            var data = $("#form-add").serializeArray();
            var count;
            for (var i = 0; i < dataList.length; i++) {
                if (!judge(i)) {
                    $.modal.alertWarning("下拉框不能为空！");
                    $("select").select2();
                    return false
                } else {
                    $.ajax({
                        url: prefix + '/uniquenBackPay',
                        data: {
                            years: dataList[i].years,
                            months: dataList[i].months,
                            compId: dataList[i].compName
                        },
                        async: false,
                        type: 'POST',
                        success: function (res) {
                            if (res.data > 0) {
                                $.modal.alertWarning("存在重复时间，请重新选择！");
                                return;
                            }
                            count = res.data;
                        }
                    });
                }
            }
            if (count == 0) {
                $.operate.saveTab(prefix + '/backPayAdd', data);
            }
        }

    }
</script>
</body>
</html>
